﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.IO;

namespace EP2_Gameserver
{
    public static class MemoryStreamExtensions
    {


        public static void AddByte(this MemoryStream stream, byte value)
        {
            stream.AddBytes(new[] { value });
        }

        public static void AddBytes(this MemoryStream stream, byte[] values)
        {
            stream.Write(values, 0, values.Length);
        }

        public static void AddInt64(this MemoryStream stream, Int64 value)
        {
            stream.Write(BitConverter.GetBytes(value).Reverse().ToArray(), 0, BitConverter.GetBytes(value).Reverse().ToArray().Length);
        }

        public static void AddInt32(this MemoryStream stream, int value)
        {
            stream.Write(BitConverter.GetBytes(value).Reverse().ToArray(), 0, BitConverter.GetBytes(value).Reverse().ToArray().Length);
        }

        public static void AddInt32r(this MemoryStream stream, int value)
        {
            stream.Write(BitConverter.GetBytes(value), 0, BitConverter.GetBytes(value).Length);
        }

        public static void AddInt16(this MemoryStream stream, Int16 value)
        {
            stream.Write(BitConverter.GetBytes(value).Reverse().ToArray(), 0, BitConverter.GetBytes(value).Reverse().ToArray().Length);
        }

        public static void AddFloat(this MemoryStream stream, float value)
        {
            stream.Write(BitConverter.GetBytes(value), 0, 4);
        }

        public static void AddString(this MemoryStream stream, string value)
        {
            System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
            stream.Write(encoding.GetBytes(value), 0, encoding.GetBytes(value).Length);
        }

        public static void SendPack(this NetworkStream stream, MemoryStream value)
        {
            Program.MessageCount++;

            byte[] Data = value.ToArray();
            byte[] Header = { 0x01, 0x81, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, Convert.ToByte(Data.Length + 2 + 5)};
            byte[] EncryptedHeader = { 0x01, 0x00, 0x00, 0x00, Convert.ToByte(Data.Length) };
            byte[] Key = { 0x00, 0x00 };

            MemoryStream Package = new MemoryStream();
            //Package.AddBytes(Header);
            //Package.AddBytes(EncryptedHeader);

            /* Header */
            Package.AddByte(0x01);
            Package.AddByte(0x81);
            Package.AddInt32(Program.MessageCount);
            Package.AddByte(0x00);
            Package.AddByte(0x00);
            Package.AddInt32(Data.Length + 2 + 5);
            /* End Header */

            /* Encrypted Header */
            Package.AddByte(0x01);
            Package.AddInt32(Data.Length);
            /* End Encrypted Header */

            Package.AddBytes(Data);

            Package.AddBytes(Key);

            stream.Write(Package.ToArray(), 0, Package.ToArray().Length);

        }

        public static string ReadString(this byte[] stream, int position)
        {
            int a = 0;

            System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();


            while (stream[position] != 0x00)
            {
                a++;
                position++;
            }

            byte[] text = new byte[a];
            Array.Copy(stream, position - a, text, 0, a);

            return encoding.GetString(text);

        }
        public static void SendPack(this NetworkStream stream, Byte[] value)
        {
            byte[] Data = value;
            byte[] Heade = { 0x01, 0x81, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
            byte[] EncryptedHeade = { 0x01 };
            byte[] Key = { 0x00, 0x00 };

            MemoryStream Head = new MemoryStream();
            Head.AddBytes(Heade);
            Head.AddInt32(Data.Length + 2 + 5);
            byte[] Header = Head.ToArray();

            MemoryStream enchd = new MemoryStream();
            enchd.AddBytes(EncryptedHeade);
            enchd.AddInt32(Data.Length);
            byte[] EncryptedHeader = enchd.ToArray();


            MemoryStream Package = new MemoryStream();
            Package.AddBytes(Header);
            Package.AddBytes(EncryptedHeader);
            Package.AddBytes(Data);
            Package.AddBytes(Key);

            stream.Write(Package.ToArray(), 0, Package.ToArray().Length);

         }

    }
}
